package com.example.liliang.rediogrouptest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class ToggleActivity extends AppCompatActivity {

    private ToggleButton toggleButton;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toggle);
        toggleButton = findViewById(R.id.tb_off);
        img = findViewById(R.id.img);
        setListener();
    }

    private void setListener() {
        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (toggleButton.isChecked()) {
                    img.setImageResource(R.drawable.ic_launcher_background);
                    Toast.makeText(ToggleActivity.this,toggleButton.getText().toString(),Toast.LENGTH_SHORT).show();
                } else {
                    img.setImageResource(R.drawable.ic_launcher_foreground);
                    Toast.makeText(ToggleActivity.this,toggleButton.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
